package com.h3c.nem.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.dto.GenerateOfferDTO;
import com.h3c.nem.entity.SdDept;
import com.h3c.nem.entity.SdUser;
import com.h3c.nem.entity.TbOfferSendRecord;
import com.h3c.nem.mapper.SdUserMapper;
import com.h3c.nem.mapper.TbOfferSendRecordMapper;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ISdDeptService;
import com.h3c.nem.service.ISdUserService;
import com.h3c.nem.service.ITbOfferSendRecordService;
import com.h3c.nem.utils.AppBusinessException;
import com.h3c.nem.utils.BusiEnumDefine;
import com.h3c.nem.vo.OfferSendRecordVO;
import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * offer发放记录(TbOfferSendRecord)表服务实现类
 *
 * @author makejava
 * @since 2022-06-08 09:15:26
 */
@Service
public class TbOfferSendRecordServiceImpl extends ServiceImpl<TbOfferSendRecordMapper, TbOfferSendRecord> implements ITbOfferSendRecordService {

    @Resource
    private TbOfferSendRecordMapper tbOfferSendRecordMapper;

    @Resource
    private CommService commService;
    @Resource
    private ISdDeptService iSdDeptService;
    @Resource
    private SdUserMapper sdUserMapper;

    @Override
    public PageInfo<OfferSendRecordVO> getOfferSendRecord(Integer currentPage, Integer pageSize, String workerIds,
                                                      String startDate, String endDate) {
        List<String> list = null;
        if (StringUtils.isNotBlank(workerIds)) {
            list = Arrays.asList(workerIds.split(","));
        }

        //获取bp的管理部门链路集合
        List<String> nameChainList;
        String loginUserRole = commService.getLoginUserRole();
        if (loginUserRole.contains(String.valueOf(BusiEnumDefine.USER_ROLE_OFFER_BP))
                || loginUserRole.contains(String.valueOf(BusiEnumDefine.USER_ROLE_NEW_WORKER_BP))) {
            String userId = commService.getLoginUserId();
            SdUser sdUser = sdUserMapper.selectById(userId);
            if (sdUser == null || sdUser.getCanSendOffer() == null || sdUser.getCanSendOffer() != 1) {
                throw new AppBusinessException("没有权限");
            }
            nameChainList = iSdDeptService.getBpManageDeptNameChainList(SdDept::getBpUserId);
        } else {
            throw new AppBusinessException("没有权限");
        }

        //分页结果
        Page<OfferSendRecordVO> page = PageHelper.startPage(currentPage, pageSize);
        List<OfferSendRecordVO> sendRecord = tbOfferSendRecordMapper.getOfferSendRecord(list, startDate, endDate,
                nameChainList, null);
        PageInfo<OfferSendRecordVO> pageInfo = page.toPageInfo();
        pageInfo.setList(sendRecord);
        return pageInfo;
    }

    @Override
    public String saveOfferSendRecord(GenerateOfferDTO generateOfferDTO,String content) {
        remove(new QueryWrapper<TbOfferSendRecord>().lambda()
                .eq(TbOfferSendRecord::getWorkerId,generateOfferDTO.getWorkerId())
                .eq(TbOfferSendRecord::getStatus,false)
        );
        TbOfferSendRecord offerSendRecord = new TbOfferSendRecord();
        BeanUtils.copyProperties(generateOfferDTO,offerSendRecord);
        String recordId = IdWorker.nextId() + "";
        offerSendRecord
                .setSendTime(new Date())
                .setRecordId(recordId)
                .setSendTemplateContent(content)
                .setOperatorInfo(commService.getLoginUserIdAndName());
        save(offerSendRecord);
        return recordId;
    }

    @Override
    public void updateOfferSendRecord(String workerId, List<File> fileList) {
        TbOfferSendRecord sendRecord = getOne(new QueryWrapper<TbOfferSendRecord>().lambda()
                .eq(TbOfferSendRecord::getWorkerId, workerId)
                .eq(TbOfferSendRecord::getStatus,false)
        );
        for (File file : fileList) {
            commService.uploadFileToSfs(BusiEnumDefine.OFFER_ATTACHMENT,file,sendRecord.getRecordId());
            FileUtil.del(file);
        }
        updateById(sendRecord.setStatus(true).setSendTime(new Date()));
    }
}

